#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

struct ListNode* removeElements(struct ListNode* head, int val)
{
    struct ListNode* guard = (struct ListNode*)malloc(sizeof(struct ListNode));
    guard->next = head;
    struct ListNode* cur = guard;
    while (cur->next != NULL)
    {
        if (cur->next->val == val)
        {
            struct ListNode* tail = cur->next;
            cur->next = cur->next->next;
            free(tail);
            tail = NULL;
        }
        else
        {
            cur = cur->next;
        }
    }
    return guard->next;
}